import Vue from "vue";
import Router from "vue-router";
import designer from "./designer";
import performance from "./performance";
import login from "./login";
import { getToken, getRole } from "@/utils/auth.js";
Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: "/",
      redirect: "/touristLogin"
    },
    {
      path: "/clockIn",
      name: "clockIn",
      component: () => import("@/pages/clockIn/index.vue")
    },
    {
      path: "/timekeep",
      name: "timekeep",
      component: () => import("@/pages/timekeep/index.vue")
    },
    {
      path: "/service_scope",
      name: "service_scope",
      component: () => import("@/pages/service_scope/index.vue")
    },
    ...designer,
    ...performance,
    ...login
  ]
});

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    if (getToken()) {
      if (getRole() == 2 || getRole() == 3) {
        next({
          path: "/performance/homePage"
        });
      } else {
        next({
          path: "/designer/homePage"
        });
      }
    } else {
      next();
      // next({path:'/touristLogin'});
    }
  } else {
    next();
    // if (to.meta.forget) {
    //   next();
    // } else {
    //   if (!getToken()) {
    //     next({
    //       path: "/login"
    //     });
    //   } else {
    //     next();
    //   }
    // }
  }
});
export default router;
